import { loginCode } from '@/apis/login';
import { useUserInfoApi } from '@/apis/member';
import http from '@/apis/utils/http';
import cache from '@/utils/cache';
import { defineStore } from 'pinia'

export const useUserStore = defineStore('userStore', {
  state: () => ({
	  user: {
		  licensePlate:"",
		  id:"",
		  name: "",
		  headImage: "",
		  mobile: "",
		  status: 1,
		  contact:"",
		  fleetCaptain:0
	  },
	  token: cache.getToken(),
	  refreshToken: ""
  }),
  actions: {
	  setInfo(val: any){
		  this.user = val
	  },
	  setToken(val: any) {
	    this.token = val
	    cache.setToken(val)
	  },
	  setRefreshToken(val: any) {
	    this.refreshToken = val
	    cache.setRefreshToken(val)
	  },
	  async userLoginAction(user: any) {
	    const {Token} = await loginCode(user)
	    // console.log('userLoginAction', data)
	   this.setToken(Token)
	   // this.setOpenid(data.openid)
	    await Promise.resolve()
	  },
	  async getUserInfoAction() {
	    try {
	      const data = await useUserInfoApi()
		  this.user.name = data.name
		  this.user.mobile = data.mobile
		  this.user.status = data.status
		   this.user.contact = data.contact
		    this.user.id = data.id
			this.user.licensePlate = data.licensePlate
			this.user.fleetCaptain = data.fleetCaptain
		  if(data.status === 2){
			uni.switchTab({
				url: "/pages/index/index"
			})
		  }else if(data.status === 3){
			  uni.navigateTo({
			  	url: "/pages/authentication/loading"
			  })
		  }else{
			uni.navigateTo({
				url: "/pages/authentication/authentication?info=" + encodeURIComponent(JSON.stringify(data))
			})
		  }
	    } catch (err) {
	      console.log(err)
	    }
	  },
	
	  
  }
})